<template>
  <page-header-wrapper :title="false">
    <a-card :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :xl="6" :md="12" :sm="24">
              <a-form-item label="标题">
                <a-input v-model="queryParam.title" placeholder="请输入"/>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :md="12" :sm="24">
              <a-form-item label="类型">
                <a-select v-model="queryParam.type" placeholder="请选择">
                  <a-select-option value="0">工作</a-select-option>
                  <a-select-option value="1">生活</a-select-option>
                  <a-select-option value="2">学习</a-select-option>
                  <a-select-option value="3">情感</a-select-option>
                  <a-select-option value="4">其他</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :md="12" :sm="24">
              <a-form-item label="创建时间">
                <a-date-picker v-model="queryParam.createTime" placeholder="请选择"/>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :md="12" :sm="24">
              <span class="table-page-search-submitButtons" :style="{ float: 'left', overflow: 'hidden' } || {} ">
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <div class="table-operator">
        <a-button type="primary" icon="plus" @click="$refs.modal.init()">新增</a-button>
        <a-dropdown v-if="selectedRowKeys.length > 0">
          <a-menu slot="overlay">
            <a-menu-item key="1"><a-icon type="delete" />删除</a-menu-item>
            <!-- lock | unlock -->
            <a-menu-item key="2"><a-icon type="lock" />锁定</a-menu-item>
          </a-menu>
          <a-button style="margin-left: 8px">
            批量操作 <a-icon type="down" />
          </a-button>
        </a-dropdown>
      </div>

      <s-table
        ref="table"
        size="default"
        rowKey="_id"
        :columns="columns"
        :data="loadData"
        :rowSelection="{ selectedRowKeys: this.selectedRowKeys, onChange: this.onSelectChange }"
      >
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 }}
        </span>
        <span slot="type" slot-scope="text">
          {{text | statusFilter}}
        </span>
        <span slot="description" slot-scope="text">
          <ellipsis :length="20" tooltip>{{ text }}</ellipsis>
        </span>
        <template slot="action" slot-scope="text, record">
          <a @click="$refs.modal.init(record)">编辑</a>
          <a-divider type="vertical" />
          <a @click="del(record)">删除</a>
        </template>
      </s-table>

      <table-form ref="modal" @refresh="$refs.table.refresh()"/>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import moment from "moment"
import { fetchList, delObj } from '@/api/king/article'
import TableForm from './modules/articleForm'

const statusMap = {
  0: { status: '0', text: '工作' },
  1: { status: '1', text: '生活' },
  2: { status: '2', text: '学习' },
  3: { status: '3', text: '情感' },
  3: { status: '4', text: '其他' }
}

export default {
  components: {
    TableForm
  },
  data () {
    return {
      // 表头
      columns: [
        { title: '#', scopedSlots: { customRender: 'serial' }, width: 65 },
        { title: '标题', dataIndex: 'title', scopedSlots: { customRender: 'description' }, width: 200 },
        { title: '描述', dataIndex: 'description', scopedSlots: { customRender: 'description' }, width: 200 },
        { title: '类型', dataIndex: 'type', scopedSlots: { customRender: 'type' }, width: '100px' },
        { title: '创建时间', dataIndex: 'createTime', sorter: true, width: 200 },
        { table: '操作', dataIndex: 'action', width: '120px', scopedSlots: { customRender: 'action' } }
      ],
      // 查询参数
      queryParam: {},
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        if(this.queryParam.createTime) this.queryParam.createTime = moment(this.queryParam.createTime).format('YYYY-MM-DD')
        const requestParameters = Object.assign(parameter, this.queryParam)
        return fetchList(requestParameters).then(res => {
          console.log('list', res.result)
          return res.result
        })
      },
      // 选择框
      selectedRowKeys: [],
      selectedRows: []
    }
  },
  filters: {
    statusFilter (type) {
      return statusMap[type].text
    },
    statusTypeFilter (type) {
      return statusMap[type].status
    }
  },
  methods: {
    del (row) {
      var that = this
      this.$confirm({
        title: '警告',
        content: `真的要删除 ${row.title} 吗?`,
        okText: '删除',
        okType: 'danger',
        cancelText: '取消',
        onOk () {
          delObj(row._id).then(res => {
            that.$message.success('删除成功')
            that.$refs.table.refresh(true)
          })
        },
        onCancel () {
          console.log('Cancel')
        }
      })
    },
    // 选择框
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
  },
}
</script>

<style lang="less" scoped>
  .search {
    margin-bottom: 54px;
  }

  .fold {
    width: calc(100% - 216px);
    display: inline-block
  }

  .operator {
    margin-bottom: 18px;
  }

  @media screen and (max-width: 900px) {
    .fold {
      width: 100%;
    }
  }
</style>
